package vn.game;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.EditText;
import android.view.View;

public class ExtEditText extends EditText implements TextWatcher {
	private String tag = "ExtEditText.";
	private int numEdt;
	private int idx;
	private int pos;
	private boolean across;
	private String cText;
	private int numEdtShow = -1;

	private Context context;
	String str = "";

	public ExtEditText(Context context) {
		super(context);
		this.context = context;
		numEdt = -1;
		numEdtShow = -1;
		pos = -1;
		across = false;
		cText = "";
		setLines(1);
		setBackgroundColor(Color.WHITE);
		setPadding(0, 0, 0, 0);
		setGravity(Gravity.CENTER);
		setTextSize(18);
		clearFocus();

		setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				Log.i(tag, "onLongClick");
				return false;
			}

		});

		setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.i(tag, "onClick");
			}

		});

		// setOnKeyListener(new OnKeyListener() {
		//
		// @Override
		// public boolean onKey(View v, int keyCode, KeyEvent event) {
		// Log.i(tag, "key down.....");
		// return false;
		// }
		// });

	}

	@Override
	public void onWindowFocusChanged(boolean hasWindowFocus) {
		// TODO Auto-generated method stub
		// Log.i(tag,"onWindowFocusChanged....");
		super.onWindowFocusChanged(hasWindowFocus);
	}

	@Override
	public void addTextChangedListener(TextWatcher watcher) {

		// public void onTextChanged(CharSequence s, int start, int before, int count) {
		// // TODO Auto-generated method stub
		//
		// }
		//
		// public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		// // TODO Auto-generated method stub
		//
		// }
		//
		//
		// public void afterTextChanged(Editable str) {
		// try {
		// if (str.toString().trim().equals(""))
		// return;
		//
		// ExtEditText edt = (ExtEditText) mainA.findViewById(idEdit);
		// if (edt == null)
		// return;
		// // set caps lock
		// char[] cc = str.toString().trim().toCharArray();
		// if (Character.isLowerCase(cc[0])) {
		// edt.setText(str.toString().toUpperCase());
		// }
		// } catch (Exception ex) {
		// Log.e(tag, "afterTextChanged() Error: " + ex.getMessage());
		// }
		// }
		//
		super.addTextChangedListener(watcher);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		try {

			// setText(getId()+"");
			Paint paint = new Paint();

			if (numEdtShow > 0) {
				paint.setColor(Color.BLUE);
				// // draw number on text of grid
				canvas.drawText(numEdtShow + "", 2, 18, paint);
				// paint.setColor(Color.RED);
				// canvas.drawText(pos + "", 22, 18, paint);

			}
			paint.setColor(Color.BLACK);
			canvas.drawLine(0, 0, 0, 100, paint);
			super.onDraw(canvas);
		} catch (Exception ex) {
			Log.i(tag + "onDraw", "Error: " + ex.getMessage());
		}
	}

	public void setIdx(int idx) {
		this.idx = idx;
	}

	public int getIdx() {
		return idx;
	}

	public void setPos(int pos) {
		// Log.i(tag,"num,pos: " + numEdt +", " + pos);
		this.pos = pos;
	}

	public int getPos() {
		return pos;
	}

	public void setNumEdt(int numEdt) {
		this.numEdt = numEdt;
	}

	public void setNumEdtShow(int numEdtShow) {
		this.numEdtShow = numEdtShow;
	}

	public int getnumEdt() {
		return numEdt;
	}

	public void setAcross(boolean across) {
		this.across = across;
	}

	public boolean isAcross() {
		return this.across;
	}

	public void setchar(String cText) {
		this.cText = cText;
	}

	public String getChar() {
		return cText;
	}

	@Override
	public void afterTextChanged(Editable str) {

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		String str1;
		str1 = getText().toString();
		if (str1.equals("") || str1.equals(str))
			return;
		str = str1;
		// Log.i(tag, "onTextChanged str" + str);
		setText(getText().toString().toUpperCase());
		setSelection(getText().toString().length());

		// set next focus
		if (MainPlayActivity.acrossText) {
			if (getId() % MainPlayActivity.column == 0)
				return;
			ExtEditText edt = (ExtEditText) ((Activity) context).findViewById(getId() + 1);
			if (edt != null && edt.getVisibility() == View.VISIBLE) {
				edt.requestFocus();
//				Log.i(tag, "set focus edittext");
			}
		} else {
			ExtEditText edt = (ExtEditText) ((Activity) context).findViewById(getId() + MainPlayActivity.column);
			if (edt != null && edt.getVisibility() == View.VISIBLE) {
				edt.requestFocus();
//				Log.i(tag, "set focus edittext");
			}

		}
		// ExtEditText edt = (ExtEditText) this.findViewById(getid());

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		Log.i(tag, "key down.....keycode:" + keyCode + "; acrossText:" + MainPlayActivity.acrossText);
		// back
		if (keyCode == 67) {
			if (MainPlayActivity.acrossText) {
				if (getText().toString().trim().equals("")) {
					ExtEditText edt = (ExtEditText) ((Activity) context).findViewById(getId() - 1);
					if (edt != null && edt.getVisibility() == View.VISIBLE) {
						edt.setText("");
						edt.requestFocus();
						Log.i(tag, "set focus edittext");
					}
				} else
					setText("");
			} else {
				ExtEditText edt = (ExtEditText) ((Activity) context).findViewById(getId() - MainPlayActivity.column);
				if (edt != null && edt.getVisibility() == View.VISIBLE) {
					edt.setText("");
					edt.requestFocus();
					Log.i(tag, "set focus edittext");
				}
			}
		}
		return super.onKeyDown(keyCode, event);

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		MainPlayActivity.acrossText = across;
		Log.i(tag, "onTouchEvent Edit...");
		return super.onTouchEvent(event);
	}

	// @Override
	// public boolean onTouchEvent(MotionEvent event) {
	// // ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(this, 0);
	// Activity a= (Activity)context;
	// requestFocus();
	// ((InputMethodManager) a.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(this, 1);
	// // requestFocus();
	// Log.i(tag, "onTouchEvent...." + this.getId());
	// // setText("x");
	//
	// // int kEvent;
	// // kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
	// // Activity a = (Activity)context;
	// // a.onKeyDown(kEvent, null);
	//
	// // return super.onTouchEvent(event);
	//
	// return false;
	// }

}